package com.one.group.admin.controller;

import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.admin.service.impl.SpBuildingServiceImpl;
import com.one.group.admin.service.impl.SpBuildingtypeServiceImpl;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.SpBuildingAddDto;
import com.one.group.model.dto.SpBuildingUpdateDto;
import com.one.group.model.entity.SpBuilding;
import com.one.group.model.entity.SpBuildingtype;
import com.one.group.result.Result;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spBuilding")
@Tag(name = "楼宇接口")
public class SpBuildingController {
    @Autowired
    private SpBuildingtypeServiceImpl spBuildingtypeService;
    @Autowired
    private SpBuildingServiceImpl spBuildingService;
    @GetMapping("/typeList/{communityId}")
    @Operation(summary = "查询当前小区所有楼栋类型")
    public Result typeList(@PathVariable Long communityId){
        //编写查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(SpBuildingtype::getCellId,communityId);
        List<SpBuildingtype> buildingtypeList =spBuildingtypeService.list(queryWrapper);
        if (buildingtypeList == null){
            return ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtil.success(buildingtypeList);
    }
    @GetMapping("/building/{buildingtypeId}")
    @Operation(summary = "查询当前楼栋类型下所有楼栋")
    public Result building(@PathVariable Long buildingtypeId){
        //编写查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(SpBuilding::getBuildingtypeId,buildingtypeId);
        List<SpBuilding> buildingList =spBuildingService.list(queryWrapper);
        if (buildingList == null){
            return ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtil.success(buildingList);
    }
    @PostMapping("/create")
    @Operation(summary = "创建楼栋")
    public Result create(@RequestBody SpBuildingAddDto spBuildingAddDto){
        if (spBuildingService.createBuilding(spBuildingAddDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PutMapping("/update")
    @Operation(summary = "更新楼栋")
    public Result update(@RequestBody SpBuildingUpdateDto spBuildingUpdateDto){
        if (spBuildingService.updateBuilding(spBuildingUpdateDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除楼栋")
    public Result delete(@PathVariable Long id){
        if(spBuildingService.myRemoveById(id)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
}
